package com.hong.servicehosp.controller.api;

import com.hong.commonutil.result.Result;
import com.hong.model.model.hosp.Department;
import com.hong.model.model.hosp.HospitalSet;
import com.hong.model.vo.hosp.DepartmentQueryVo;
import com.hong.model.vo.hosp.DepartmentVo;
import com.hong.servicehosp.service.DepartmentService;
import com.hong.servicehosp.service.HospitalService;
import com.hong.servicehosp.service.HospitalSetService;
import com.hong.serviceutil.common.helper.HttpRequestHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RequestMapping("/api/hosp/")
@RestController
//上传科室接口
public class ApiDepartmentController {
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation("根据医院编号获取医院的科室")
    @GetMapping("hospital/department/{hoscode}")
    public Result index(@PathVariable String hoscode) {
        List<DepartmentVo> departmentVos = departmentService.getDeptListByHosCode(hoscode);
        return Result.ok(departmentVos);
    }

    @ApiOperation("上传科室信息")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        String hospSign = (String) paramMap.get("sign");
        //根据传递过来的医院编码，查询数据库，查询签名
        String hostCode = (String) paramMap.get("hoscode");
        hospitalSetService.checkHospSign(hostCode, hospSign);
        departmentService.save(paramMap);
        return Result.ok();
    }

    @ApiOperation("查询科室")
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request) {

        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        String hostCode = (String) paramMap.get("hoscode");
        String pNumS = (String) paramMap.get("page");
        String pSizeS = (String) paramMap.get("limit");
        Integer pNum = pNumS == null ? 1 : Integer.valueOf(pNumS);
        Integer pSize = pSizeS == null ? 1 : Integer.valueOf(pSizeS);
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hostCode);
        Page<Department> pageModel = departmentService.findPageDepartment(pNum, pSize, departmentQueryVo);
        return Result.ok(pageModel);
    }

    @ApiOperation("删除科室")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        String hostCode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        String hospSign = (String) paramMap.get("sign");
        hospitalSetService.checkHospSign(hostCode, hospSign);
        departmentService.remove(hostCode, depcode);
        return Result.ok();
    }

}
